package servlet.servlet_yaobo;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import pojo.MembershipManagement;
import pojo.User;
import service.impl.ImpShoppingMemberService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 导出数据
 * 姚波
 */
@WebServlet(value = "/export")
public class ExportServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String idse=req.getParameter("ids");
        ImpShoppingMemberService imp=new ImpShoppingMemberService();
        if(idse!=null){
            String[] index=idse.split(",");
            List<User> user=new ArrayList<>();
            for(String ins:index){
                //获取指定的对象
                user.add(imp.export(Integer.parseInt(ins)));
            }
            String[] title={"会员id","账号","密码","昵称","性别","手机号","邮箱","地址","积分","时间"};
            HSSFWorkbook work=new HSSFWorkbook();
            HSSFSheet sheet=work.createSheet();
            HSSFRow row=sheet.createRow(0);
            HSSFCell cell=null;
            for(int i=0;i<title.length;i++){

                cell=row.createCell(i);
                cell.setCellValue(title[i]);
            }
            for(int i=0;i<user.size();i++){
                HSSFRow nextrow=sheet.createRow(i+1);
                HSSFCell dateCell=nextrow.createCell(0);
                dateCell.setCellValue(user.get(i).getuID());
                dateCell=nextrow.createCell(1);
                dateCell.setCellValue(user.get(i).getuID());

                dateCell=nextrow.createCell(2);
                dateCell.setCellValue(user.get(i).getuAddress());

                dateCell=nextrow.createCell(3);
                dateCell.setCellValue(user.get(i).getuNickName());

                dateCell=nextrow.createCell(4);
                dateCell.setCellValue(user.get(i).getuGender()==0?"男":"女");

                dateCell=nextrow.createCell(5);
                dateCell.setCellValue(user.get(i).getuPhoneNumber());

                dateCell=nextrow.createCell(6);
                dateCell.setCellValue(user.get(i).getuEmail());

                dateCell=nextrow.createCell(7);
                dateCell.setCellValue(user.get(i).getuAddress());

                dateCell=nextrow.createCell(8);
                dateCell.setCellValue(user.get(i).getuIntegral());
                if(user.get(i).getuRegdate()!=null){
                    dateCell=nextrow.createCell(9);
                    dateCell.setCellValue(user.get(i).getuRegdate());
                }
            }
            OutputStream output=resp.getOutputStream();
            resp.reset();
            resp.setHeader("Content-disposition","attachment; filename=admins.xls");
            resp.setContentType("application/msexcel");
            work.write(output);
            output.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
